package com.imay.live.utils;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.imay.live.R;
import com.imay.live.helper.ValueCallback;

import java.io.File;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by jeaman on 2017/9/12.
 */

public class GlideUtil {
    private final static int DEFAULT_PLACEHOLDER_RES_ID = R.drawable.shape_placeholder_2;   //默认占位图

    private static RequestOptions requestOptions(int resId) {
        return requestOptions(resId, null);
    }

    private static RequestOptions requestOptions(int resId, Transformation transformation) {
        RequestOptions requestOptions = new RequestOptions();
        if (transformation != null) {
            requestOptions.transform(transformation);
        }
        requestOptions.placeholder(resId).error(resId);
        return requestOptions;
    }

    private static void loadImage(ImageView imageView, Object url, RequestOptions requestOptions) {
        loadImage(imageView, url, requestOptions, null);
    }

    private static void loadImage(ImageView imageView, Object url, RequestOptions requestOptions, Float sizeMultiplier) {
        RequestBuilder requestBuilder = Glide.with(imageView.getContext())
                .load(url)
                .apply(requestOptions);
        if (sizeMultiplier != null) {
            requestBuilder.thumbnail(sizeMultiplier);
        }
        requestBuilder.into(imageView);
    }

    public static void downloadImage(Context context, String url, final ValueCallback<File> callback) {
        downloadImage(context, url, 0, 0, callback);
    }

    public static void downloadImage(Context context, String url, int width, int height, final ValueCallback<File> callback) {
        if (width == 0 || height == 0) {
            width = Target.SIZE_ORIGINAL;
            height = Target.SIZE_ORIGINAL;
        }
        Glide.with(context).downloadOnly().load(url).apply(new RequestOptions()).listener(new RequestListener<File>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
                if (callback != null) {
                    callback.onReceiveValue(resource);
                }
                return false;
            }
        }).submit(width, height);
    }

    private static int getDimen(Context context, int resId) {
        return context.getResources().getDimensionPixelSize(resId);
    }

    private static int getColor(Context context, int resId) {
        return context.getResources().getColor(resId);
    }

    //设置ImageView圆形外框背景
    private static void setCircleBorder(ImageView imageView, @DimenRes int borderWidthResId, @ColorRes int borderColorResId) {
        int borderWidth = getDimen(imageView.getContext(), borderWidthResId);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.OVAL);
        drawable.setStroke(borderWidth, getColor(imageView.getContext(), borderColorResId));
        imageView.setPadding(borderWidth, borderWidth, borderWidth, borderWidth);
        imageView.setBackground(drawable);
    }

    private static void setRectangleBorder(ImageView imageView, @DimenRes int borderWidthResId, @ColorRes int borderColorResId, @DimenRes int radiusResId) {
        int borderWidth = getDimen(imageView.getContext(), borderWidthResId);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setStroke(borderWidth, getColor(imageView.getContext(), borderColorResId));
        drawable.setCornerRadius(getDimen(imageView.getContext(), radiusResId));
        imageView.setPadding(borderWidth, borderWidth, borderWidth, borderWidth);
        imageView.setBackground(drawable);
    }

    //普通
    public static void loadImage(ImageView imageView, Object url) {
        loadImage(imageView, url, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadImage(ImageView imageView, Object url, int placeholderResId) {
        loadImage(imageView, url, requestOptions(placeholderResId));
    }

    //圆形
    public static void loadCircleImage(ImageView imageView, Object url) {
        loadCircleImage(imageView, url, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadCircleImage(ImageView imageView, Object url, int placeholderResId) {
        loadImage(imageView, url, requestOptions(placeholderResId, new CropCircleTransformation()));
    }

    //圆形带外框
    public static void loadCircleBorderImage(ImageView imageView, Object url, @DimenRes int borderWidthResId, @ColorRes int borderColorResId) {
        loadCircleBorderImage(imageView, url, borderWidthResId, borderColorResId, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadCircleBorderImage(ImageView imageView, Object url, @DimenRes int borderWidthResId, @ColorRes int borderColorResId, int placeholderResId) {
        setCircleBorder(imageView, borderWidthResId, borderColorResId);
        loadImage(imageView, url, requestOptions(placeholderResId, new CropCircleTransformation()));
    }

    //方形圆角
    public static void loadRectangleImage(ImageView imageView, Object url, @DimenRes int radiusResId) {
        loadRectangleImage(imageView, url, radiusResId, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadRectangleBorderImage(ImageView imageView, Object url, @DimenRes int borderWidthResId, @ColorRes int borderColorResId, @DimenRes int radiusResId) {
        setRectangleBorder(imageView, borderWidthResId, borderColorResId, radiusResId);
        loadRectangleImage(imageView, url, radiusResId, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadRectangleImage(ImageView imageView, Object url, @DimenRes int radiusResId, int placeholderResId) {
        int radius = getDimen(imageView.getContext(), radiusResId);
        loadImage(imageView, url, requestOptions(placeholderResId, new MultiTransformation<>(new CenterCrop(), new RoundedCornersTransformation(radius, 0))));
    }

    //高斯模糊
    public static void loadBlurImage(ImageView imageView, Object url, int radius, int sampling) {
        loadBlurImage(imageView, url, radius, sampling, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadBlurImage(ImageView imageView, Object url, int radius, int sampling, int placeholderResId) {
        loadImage(imageView, url, requestOptions(placeholderResId, new BlurTransformation(radius, sampling)).dontAnimate());
    }

    public static void loadThumbnailImage(ImageView imageView, Object url, float sizeMultiplier) {
        loadThumbnailImage(imageView, url, sizeMultiplier, DEFAULT_PLACEHOLDER_RES_ID);
    }

    public static void loadThumbnailImage(ImageView imageView, Object url, float sizeMultiplier, int placeholderResId) {
        loadImage(imageView, url, requestOptions(placeholderResId), sizeMultiplier);
    }

    public static void loadWebpImage(SimpleDraweeView draweeView,String url, int h, int w,boolean auto){
        PipelineDraweeControllerBuilder pipelineDraweeControllerBuilder = Fresco.newDraweeControllerBuilder()
                .setUri(url);
        if (h > 0) {
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                    .setResizeOptions(new ResizeOptions(w, h)).build();
            pipelineDraweeControllerBuilder.setImageRequest(request);
        }
        DraweeController controller =
                pipelineDraweeControllerBuilder
//                        .setOldController(draweeView.getController())
                        .setAutoPlayAnimations(auto)
                        .build();
        draweeView.setController(controller);
    }
}
